package org.ala.tiktools.rpc.kcp.protocol.sender.queue;

import org.ala.tiktools.rpc.kcp.protocol.segment.KcpSegment;
import org.ala.tiktools.tools.list.DoubleLinkedList;

/**
 * @author ala
 * @date 2025-04-13 11:32
 */
public class KcpSenderLinkedList extends DoubleLinkedList<KcpSegment> {


    /**
     *  确认sn号
     */
    public void parseAck(long sn) {
        Node<KcpSegment> crt = HEAD.next;
        while (crt != TAIL) {
            if (sn == crt.val.getSn()) {
                remove(crt);
                break;
            }
            if (sn < crt.val.getSn()) {
                break;
            }
            crt = crt.next;
        }
    }

    /**
     *  处理fastack
     *  <p> sndBuf中 < ackSn 的 消息，fastAck标记累加一次
     */
    public void parseFastack(long ackSn) {
        Node<KcpSegment> crt = HEAD.next;
        while (crt != TAIL) {
            if (ackSn < crt.val.getSn()) { break; }
            //  后面的已经到了，前面的还没到，有理由怀疑前面的丢了
            else if (ackSn != crt.val.getSn()) {
                crt.val.incrFastAck();
            }
            //  等于的在前面就已经删掉了
        }
    }

	public Node<KcpSegment> first() {
		return HEAD.next;
	}

	public Node<KcpSegment> last() {
		return TAIL.prev;
	}

}
